//
//  SpringDetailViewController.m
//  ProjectA
//
//  Created by dlios on 15-3-6.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SpringDetailViewController.h"

@interface SpringDetailViewController ()

@end

@implementation SpringDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.myScrollerV = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myScrollerV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myScrollerV];
    
    //收藏按钮
   
    UIImage *image = [UIImage imageNamed:@"star_faved@2x.png"];
    image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:(UIBarButtonItemStylePlain) target:self action:@selector(collectionAction)];
    
    
    //-------------------------解析数据------------------------------------//
    NSString *str = [NSString stringWithFormat:@"http://api.yi18.net/cook/show?id=%@",self.id];
    
    [AFNDownLoader getDataDownLoad:str block:^(id myData) {
        NSMutableDictionary *dic = myData;
        self.model = [[SpringModel alloc] init];
        //此处是字典不是数组就只是单组数据
        NSMutableDictionary *myDic = [dic objectForKey:@"yi18"];
        //方法步骤
        //解析的数据中有符号 去掉符号
        
        self.methodLable = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 12, self.view.frame.size.height / 2.85, self.view.frame.size.width - self.view.frame.size.width / 12 * 2, self.view.frame.size.height + 300)];
        
        NSString *string = [myDic objectForKey:@"message"];
        NSString *string1 =  [string stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
        NSString *string2 = [string1 stringByReplacingOccurrencesOfString:@"<h2>" withString:@""];
        NSString *string3 = [string2 stringByReplacingOccurrencesOfString:@"</h2>" withString:@""];
        NSString *string4 = [string3 stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
        NSString *string5 = [string4 stringByReplacingOccurrencesOfString:@"<br />" withString:@""];
        NSString *string6 = [string5 stringByReplacingOccurrencesOfString:@"&nbsp" withString:@""];
        NSString *string7 = [myDic objectForKey:@"name"];
        self.navigationItem.title = string7;
        [self.model setValuesForKeysWithDictionary:myDic];
        //lable赋值
        self.methodLable.text = string6;
        self.methodLable.numberOfLines = 0;
        
        //lable自适应高度
        if (self.view.frame.size.width == 375) {
            
            CGRect frame = self.methodLable.frame;
            frame.size.height = [[self class] heightForText:string6];
            self.methodLable.frame = frame;
            self.methodLable.text = string6;
            //修改scrollView.contectSize的大小
            self.myScrollerV.contentSize = CGSizeMake(self.view.frame.size.width, frame.size.height + 250);
        }if (self.view.frame.size.width == 320) {
            CGRect frame = self.methodLable.frame;
            frame.size.height = [[self class] heightForText1:string6];
            self.methodLable.frame = frame;
            self.methodLable.text = string6;
            //修改scrollView.contectSize的大小
            self.myScrollerV.contentSize = CGSizeMake(self.view.frame.size.width, frame.size.height + 250);
        }if (self.view.frame.size.width == 414) {
            CGRect frame = self.methodLable.frame;
            frame.size.height = [[self class] heightForText2:string6];
            self.methodLable.frame = frame;
            self.methodLable.text = string6;
            //修改scrollView.contectSize的大小
            self.myScrollerV.contentSize = CGSizeMake(self.view.frame.size.width, frame.size.height + 250);
            
        }
        
        
        //赋值图片
        self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
        NSString *pictureStr = [myDic objectForKey:@"img"];
        NSString *urlStr = [NSString stringWithFormat:@"http://www.yi18.net/%@",pictureStr];
        NSURL *url = [NSURL URLWithString:urlStr];
        [self.imageV sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default.jpg"]];
        [self.myScrollerV addSubview:self.imageV];
        [self.myScrollerV addSubview:self.methodLable];
        NSLog(@"detail%@", self.id);
        
    }];
    // Do any additional setup after loading the view.
}

//自适应高度
+ (CGFloat)heightForText:(NSString *)text
{
    
    CGSize size = CGSizeMake(230, 3000);
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16.0] forKey:NSFontAttributeName];
    
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}

+ (CGFloat)heightForText1:(NSString *)text1
{
    
    CGSize size = CGSizeMake(206.7, 3000);
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16.0] forKey:NSFontAttributeName];
    
    CGRect frame = [text1 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}

+ (CGFloat)heightForText2:(NSString *)text2
{
    
    CGSize size = CGSizeMake(256.7, 3000);
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16.0] forKey:NSFontAttributeName];
    
    CGRect frame = [text2 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}


- (void)collectionAction
{
    UIActionSheet *sheetAction = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"返回" destructiveButtonTitle:nil otherButtonTitles:@"收藏",@"分享", nil];
    sheetAction.frame = CGRectMake(30, 340, 375, 200);
    [sheetAction showInView:self.view];
    
}
//收藏 分享
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    int temp = 0;
    
    if (buttonIndex == 0) {
        
            [[CollectionDataBase shareCollectionDataBase] openDB];
            [[CollectionDataBase shareCollectionDataBase] createTable];
            self.array = [[CollectionDataBase shareCollectionDataBase] selectSpring];
       
                    for (SpringModel *myData in self.array) {
                if ([myData.name isEqualToString:self.model.name ]) {
                    temp++;
                }
            
            if (temp != 0) {
                UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"注意" message:@"该食材已经被收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
                [alertV show];
    
            }
                    }
            if (temp == 0) {
                NSLog(@"%d", temp);
                UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"恭喜亲" message:@"食谱收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"返回", nil];
                [alertV show];
            [[CollectionDataBase shareCollectionDataBase] insertSpringModel:self.model];
                
            }
        }

    
// 点击分享按钮 分享到新浪微博
    if (buttonIndex == 1) {
         NSLog(@"分享");
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"507fcab25270157b37000010"
                                          shareText:[NSString stringWithFormat:@"%@  的样子看起来很好吃,推荐你也来品尝(来自上得厅堂)",self.navigationItem.title]
                                         shareImage:nil
                                      shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,nil]
                                           delegate:self];
        }
    
    self.isOn = !self.isOn;
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
